package com.nannybear.Communication;

import android.util.Log;

import com.nannybear.MainActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @brief Initialization of the connection with NannyBear.
 */
public class Connection extends Thread{
    /********** DECLARATIONS **********/
    // Variables
    private String ip;
    private int port;
    private Socket socket=null;
    private BufferedReader reader=null;
    private BufferedWriter writer=null;
    private final static String ENCODING = "UTF-8";
    private boolean isConnected;

    // Classes
    private Postman postman;
    private ProxyRobot proxyRobot;
    private MainActivity activity;

    //TEST
    private boolean wantConnection = true;

    /************ METHODS ************/
    /**
     * @brief Constructor.
     */
    public Connection(MainActivity activity, String ip, int port){
        this.activity = activity;
        this.ip = ip;
        this.port = port;
        this.isConnected = false;
    }

    /**
     * @brief Run the thread.
     */
    public void run(){
        connect();
    }

    /**
     * @brief Connection to the server. Initialization of reader and writer buffers.
     */
    public void connect(){
        if(wantConnection == true){
            try {
                this.socket=new Socket(this.ip, this.port);
                this.reader = new BufferedReader((new InputStreamReader(socket.getInputStream(), ENCODING)));
                this.writer = new BufferedWriter((new OutputStreamWriter(socket.getOutputStream(), ENCODING)));
                changeConnectionState(true);
                this.postman = new Postman(this);
                postman.start();
                this.proxyRobot = new ProxyRobot(this);
                Log.e("Connection", "OK");
            }catch (UnknownHostException ex){
                Log.e("Connection", "Problem with the IP : "+ex.getMessage());
                changeConnectionState(false);
            }catch (IOException ex){
                Log.e("Connection", "Problem with the port : "+ex.getMessage());
                changeConnectionState(false);
            }
        }
    }

    /**
     * @brief Send a message.
     * @param message : message to send
     */
    public void write(String message){
        try{
            this.writer.write(message, 0, message.length());
            this.writer.newLine();
            this.writer.flush();
        }catch (IOException ex){
            Log.e("Connection", "Problem to send a message"+ex.getMessage());
            changeConnectionState(false);
        }
    }

    /**
     * @brief Message to read.
     */
    public String read(){
        String receivedMessage = null;
        try{
            receivedMessage = this.reader.readLine();
        }catch(IOException ex){
            Log.e("Connection","Problem to read the message : "+ex.getMessage());
        }
        return receivedMessage;
    }

    /**
     * @brief Change the state of the connection.
     */
    public void changeConnectionState(boolean connectionState){
        this.isConnected = connectionState;
    }

    /**
     * @brief Close the connection.
     */
    public void closeConnection(){
        try{
            this.socket.close();
            Log.e("Connection", "socket closed");
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    /*********** ACCESSORS ***********/
    public boolean isConnected() {
        return isConnected;
    }

    public Socket getSocket() {
        return socket;
    }

    public ProxyRobot getProxyRobot() {
        return proxyRobot;
    }

    public MainActivity getActivity() {
        return activity;
    }
}
